 /**
 * Description: 门店信息
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-03-27   Hunter       1.0.0     Initial
 */
package com.metro.demo.biz.controller;

import com.github.pagehelper.PageInfo;
import com.metro.core.annotation.BusinessDesc;
import com.metro.core.excel.JxlsView;
import com.metro.core.page.Page;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.QueryParams;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.demo.biz.beans.entity.Biz4sinfoEntity;
import com.metro.demo.biz.beans.vo.BizVersionHistory;
import com.metro.demo.biz.service.inf.IBizVersionHistoryService;
import com.metro.demo.common.beans.constant.JxlsTemplateEnum;
import com.metro.demo.sys.beans.vo.SysPermission;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

import com.metro.demo.biz.service.inf.IBiz4sinfoService;
import com.metro.demo.biz.beans.vo.Biz4sinfo;
import com.metro.demo.web.controller.BaseController;
import com.metro.demo.common.utils.DicUtil;
import com.metro.demo.common.beans.constant.DicTypeEnum;

import java.util.List;

 /**
 * TV信息<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-03-27]
 */
@Controller
@RequestMapping("/biz4sinfo")
public class Biz4sinfoController extends BaseController {
    //菜单编码
    private static final String menuCode = "BIZ_4SINFO";
    //模块描述
    private static final String MODULE_DESC = "门店信息";
    @Autowired
    private IBiz4sinfoService biz4sinfoService;
    @Autowired
    private IBizVersionHistoryService bizversionhistoryService;
    @Override
    protected String getViewDir() {
        return "biz/";
    }
    
    /**
     * 去查询门店信息
     * 
     * @param biz4sinfo 门店信息页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-03-27]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询门店信息列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "list/{code}", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView list(@PathVariable String code,Biz4sinfo biz4sinfo) {
        ModelAndView mav = new ModelAndView();
        try {
            if (code.equals("1")){
                mav.setViewName(getView("biz4sinfo_list"));
            }else {
                mav.setViewName(getView("bizversioninfo_list"));
            }
            mav.addObject("upgraeds", DicUtil.getLabelValueList(DicTypeEnum.UPGRADE));
            mav.addObject("sysVersions", DicUtil.getLabelValueList(DicTypeEnum.SYS_VERSION));
            mav.addObject("searchEntity", biz4sinfo);
        } catch (Exception ex) {
            log.error("查询门店信息出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询门店信息
     *
     * @param biz4sinfo 门店信息页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-03-27]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询门店信息列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolist", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolist(Biz4sinfo biz4sinfo, Page page, QueryParams<Biz4sinfo> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setPaging(page);
            queryParams.setEntity(biz4sinfo);
            PageInfo<Biz4sinfo> pageInfo = biz4sinfoService.queryForPage(queryParams);
            jr.setData(pageInfo);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询门店信息出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 去新增门店信息
     * 
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-03-27]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去新增门店信息")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "create")
    public ModelAndView createForm(Model model, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            initCreateOrUpdateFrom(mav, "create");
        } catch (Exception ex) {
            log.error("去新增门店信息出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }
    
    /**
     * 初始化新增修改门店信息所需参数
     * @author Hunter
     */
    private void initCreateOrUpdateFrom(ModelAndView mav, String action){
        mav.addObject("upgraeds", DicUtil.getLabelValueList(DicTypeEnum.UPGRADE));
        mav.addObject("tvStatuss", DicUtil.getLabelValueList(DicTypeEnum.TVSTATUS));
        mav.addObject("sysVersions", DicUtil.getLabelValueList(DicTypeEnum.SYS_VERSION));
        mav.addObject("action", action);
        mav.setViewName(getView("biz4sinfo_form"));
    }
    
    /**
     * 新增门店信息
     * 
     * @param entity 门店信息页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-03-27]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增门店信息")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveBiz4sinfo(Biz4sinfo entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = biz4sinfoService.save(entity);
            jr.setResult(res);
            log.debug("新增门店信息成功【{}】", entity);
        } catch (Exception ex) {
            log.error("新增门店信息出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 删除门店信息
     * 
     * @param id 门店信息页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-03-27]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除门店信息")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable String id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除门店信息id={}",id);
            IResult codeMsg = biz4sinfoService.deleteByIds(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除门店信息出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去修改门店信息
     * 
     * @param id 门店信息页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-03-27]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去修改门店信息")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "edit/{id}", method = { RequestMethod.GET})
    public ModelAndView updateForm(@PathVariable Long id, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            Biz4sinfo entity = biz4sinfoService.getById(id);
            mav.addObject("entity", entity);
            initCreateOrUpdateFrom(mav,"edit");
        } catch (Exception ex) {
            log.error("去修改门店信息", ex);
            mav.setView(new RedirectView("list"));
        }
        return mav;
    }

    /**
     * 修改门店信息
     * 
     * @param entity 门店信息页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-03-27]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改门店信息")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateBiz4sinfo(Biz4sinfo entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = biz4sinfoService.update(entity);
            jr.setResult(res);
            log.debug("修改门店信息成功【{}】", entity);
        } catch (Exception ex) {
            log.error("修改门店信息出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去查看门店信息
     * 
     * @param id 门店信息页面表单对象唯一标识
     * @return 结果视图
     * @author Hunter
     * @since [1.0.0]
     * @version [1.0.0, 2018-03-27]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看门店信息详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
    public ModelAndView info(@PathVariable Long id) {
        ModelAndView mav= new ModelAndView();
        try {
            mav.addObject("entity", biz4sinfoService.getById(id));
            mav.setViewName(getView("biz4sinfo_info"));
        } catch (Exception ex) {
            log.error("查看门店信息出错", ex);
        }
        return mav;
        
    }

     @RequestMapping(value = "batchImportForm", method = RequestMethod.GET)
     public ModelAndView batchImportForm() {
         ModelAndView mav = new ModelAndView();
         mav.setViewName(getView("biz4sinfo_import"));
         return mav;
     }

     /**
      * 批量导入
      * @return
      */
     @RequestMapping(value = "/batchImport")
     @ResponseBody
     public JsonResult batchImport(String filePath, HttpServletRequest request) {
         JsonResult jr = new JsonResult();
         try {
             jr = biz4sinfoService.batchImport(filePath);
             log.debug("批量导入结果：{}", jr);
         } catch (Exception ex) {
             log.error("批量导入", ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }

     /**
      * TV信息导出
      * @return
      */
     @RequestMapping(value = "export", method = RequestMethod.GET)
     public ModelAndView export() {
         Biz4sinfo entity = null;
         List<Biz4sinfo> list = biz4sinfoService.queryForList(entity);
         return new ModelAndView(new JxlsView(JxlsTemplateEnum.BIZ4SINFO, list));
     }

     /**
      * 版本信息导出
      * @return
      */
     @RequestMapping(value = "exportVersion", method = RequestMethod.GET)
     public ModelAndView exportVersion(Biz4sinfo entity) {
         List<Biz4sinfo> list = biz4sinfoService.queryForList(entity);
         return new ModelAndView(new JxlsView(JxlsTemplateEnum.BIZVERSIONINFO, list));
     }
}
